package com.jsy.imagepicker;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.piasy.biv.BigImageViewer;
import com.github.piasy.biv.loader.fresco.FrescoImageLoader;

public class ImagePreviewActivity extends AppCompatActivity {


    Context mContext;
    ViewPager vpImg;
    Button btBack;
    TextView tvNum;
    TextView tvConfirm;
    ImageView ivCheck;
    View.OnClickListener onClickListener;
    PreviewAdapter previewAdapter;

    int count;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        BigImageViewer.initialize(FrescoImageLoader.with(mContext));
        setContentView(R.layout.activity_image_preview);
        setNoTitle();
        findViewById();
        setAdapter();
        setNum();
    }

    private void setNum() {
        count = getIntent().getIntExtra("count", 1);
        tvNum.setText(PickerAdapter.checkedList.size() + "/" + count);
        if (PickerAdapter.checkedList.size() == 0) {
            tvNum.setTextColor(Color.parseColor("#808080"));
            tvConfirm.setTextColor(Color.parseColor("#808080"));
            tvConfirm.setBackgroundResource(R.drawable.bg_item_type);
        } else {
            tvNum.setTextColor(Color.parseColor("#f0f0f0"));
            tvConfirm.setTextColor(Color.parseColor("#f0f0f0"));
            tvConfirm.setBackgroundResource(R.drawable.bg_confirm);
        }
    }

    private void setAdapter() {
        previewAdapter = new PreviewAdapter();
        vpImg.setAdapter(previewAdapter);
        vpImg.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (PickerAdapter.checkedList.contains(PickerAdapter.allList.get(position))) {
                    ivCheck.setImageResource(R.drawable.radio_checked);
                } else {
                    ivCheck.setImageResource(R.drawable.radio_unchecked);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vpImg.setCurrentItem(getIntent().getIntExtra("position", 0));

    }

    private void findViewById() {
        vpImg = findViewById(R.id.vp_img);
        btBack = findViewById(R.id.btn_back);
        tvNum = findViewById(R.id.tv_num);
        tvConfirm = findViewById(R.id.tv_confirm);
        ivCheck = findViewById(R.id.ll_check);
        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.btn_back) {
                    finish();
                } else if (v.getId() == R.id.tv_confirm) {
                    setResult(RESULT_OK);
                    finish();
                } else if (v.getId() == R.id.ll_check) {
                    String item = PickerAdapter.allList.get(vpImg.getCurrentItem());
                    if (PickerAdapter.checkedList.contains(item)) {
                        PickerAdapter.checkedList.remove(item);
                        ivCheck.setImageResource(R.drawable.radio_unchecked);
                    } else {
                        if (count == 1) {
                            PickerAdapter.checkedList.clear();
                            PickerAdapter.checkedList.add(item);
                            ivCheck.setImageResource(R.drawable.radio_checked);
                        } else {
                            if (PickerAdapter.checkedList.size() < count) {
                                PickerAdapter.checkedList.add(item);
                                ivCheck.setImageResource(R.drawable.radio_checked);
                            }
                        }

                    }
                    setNum();
                }
            }
        };
        btBack.setOnClickListener(onClickListener);
        tvConfirm.setOnClickListener(onClickListener);
        ivCheck.setOnClickListener(onClickListener);
    }

    private void setNoTitle() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        try {
            getSupportActionBar().hide();
        } catch (Exception e) {
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BigImageViewer.imageLoader().cancelAll();
    }
}
